<template>
    <section class="form-container">
        <el-form :inline="true" :model="filter" ref="filterRef" size="mini" class="form-filter">
            <el-form-item label="应用">
                <el-select v-model="filter.appId" filterable placeholder="请选择" clearable style="width:200px;" @change="getPage()">
                    <el-option
                        v-for="item in Dict.enums.appList"
                        :key="item.code"
                        :label="item.name"
                        :value="item.code"
                    />
                </el-select>
            </el-form-item>
            <el-form-item label="版本号" prop="version">
                <el-input v-model="filter.version" placeholder="请输入"/>
            </el-form-item>

            <el-form-item>
                <el-button type="primary" @click="getPage()" icon="el-icon-search">查询</el-button>
                <el-button @click="resetForm('filterRef')" icon="el-icon-refresh-left">重置</el-button>
                <el-button @click="showUpdate()" icon="el-icon-plus">添加</el-button>
            </el-form-item>
        </el-form>
        <div class="form-content">
            <el-table
                v-loading="loading"
                style="width: 100%"
                :data="page.records"
                border
                stripe
                :header-cell-style = "headerCellStyle"
            >
                <el-table-column width="60" prop="id" label="ID"/>

                <el-table-column prop="appName" label="应用"/>
                <el-table-column prop="version" label="版本号"/>
                <el-table-column prop="description" label="更新说明"/>
                <el-table-column prop="apkUrl" label="下载链接" min-width="140px">
                    <template #default="scope">
                        <el-link v-if="scope.row.apkUrl" :href="scope.row.apkUrl" target="_blank" icon="el-icon-link">
                            点击下载
                        </el-link>
                    </template>
                </el-table-column>
                <el-table-column prop="forced" label="是否强制更新">
                    <template #default="scope">
                        <el-tag v-if="scope.row.forced" type="danger">是</el-tag>
                        <el-tag v-else type="success">否</el-tag>
                    </template>
                </el-table-column>

                <el-table-column prop="updateName" label="更新人"/>
                <el-table-column prop="updateTime" label="更新时间"/>

                <el-table-column fixed="right" label="操作" width="160">
                    <template #default="scope">
                        <el-button size="mini" style="margin:0 0px 10px 10px;" @click="showUpdate(scope.row)">编辑
                        </el-button>

                        <el-popconfirm title="确定删除吗？" @onConfirm="handleDelete(scope.row.id)">
                            <el-button slot="reference" size="mini" style="margin:0 0px 10px 10px;">删除
                            </el-button>
                        </el-popconfirm>

                    </template>
                </el-table-column>
            </el-table>

            <el-pagination
                background
                :current-page="page.current"
                :page-sizes="[10, 20, 30, 50,100]"
                :page-size="page.size"
                layout="total, sizes, prev, pager, next, jumper"
                :total="page.total"
                style="margin-top:20px;"
                @size-change="handleSizeChange"
                @current-change="handleCurrentChange"
            />

            <Update ref="updateRef" @refresh="getPage()"/>
        </div>
    </section>
</template>
<script>
import {AppVersionApi} from '@/api/system/AppVersionApi'
import Update from './Update.vue'

export default {
    name: 'AppVersionList',
    components: {
        Update
    },
    data() {
        return {
            page: {
                total: 0,
                size: 10,
                current: 1,
                pages: 0,
                records: []
            },
            filter: {
                appId:'',
                version: '',
            },
            coordinate: {column: -1, row: -1},
            loading: false,
            headerCellStyle:{
                "background-color":"#F2F5FC"
            }
        }
    },
    created() {
        this.getPage()
    },
    methods: {
        // 获取用户列表
        getPage() {
            const params = Object.assign({}, this.filter);
            params['pageIndex'] = this.page.current
            params['pageSize'] = this.page.size

            this.loading = true
            AppVersionApi.getPage(params).then((response) => {
                this.page = response
                this.loading = false
            }).catch((err) => {
                this.loading = false
            })
        },

        // 删除数据
        handleDelete(id) {
            AppVersionApi.delete(id).then((response) => {
                this.getPage()
            })
        },

        handleSizeChange(val) {
            this.page.size = val
            this.page.current = 1
            this.getPage()
        },

        handleCurrentChange(val) {
            this.page.current = val
            this.getPage()
        },

        showUpdate(row) {
            this.$refs['updateRef'].showUpdate(row)
        },

        // 修改状态
        statusSwitch(val, id) {
            const params = {
                id: id,
                status: val
            }

            AppVersionApi.edit(params).then((response) => {
            })
        },
        resetForm(formName) {
            this.$refs[formName].resetFields()
            this.getPage()
        }
    }
}

</script>

<style lang="scss" scoped></style>

